package vip.web3.api.dao.biz;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import vip.web3.api.entity.biz.UserWalletEntity;
import vip.web3.common.enums.WalletTypeEnum;
import vip.web3.common.exception.RRException;

import java.math.BigDecimal;

/**
 * aut-horlilei
 */
@Mapper
public interface UserWalletDao extends BaseMapper<UserWalletEntity> {

    /**
     * 批量删除用户钱包
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteAUserWalletByIds(Integer[] ids);

    int updateAmountCny(@Param("id") Integer id,
                        @Param("amount") BigDecimal amount,
                        @Param("version") Integer version,
                        @Param("type") Integer type);

    int updateAmountUsdt(@Param("id") Integer id,
                         @Param("amount") BigDecimal amount,
                         @Param("version") Integer version,
                         @Param("type") Integer type);

    int updateWithdrawCny(@Param("id") Integer id,
                          @Param("amount") BigDecimal amount,
                          @Param("version") Integer version);

    int updateWithdrawUsdt(@Param("id") Integer id,
                           @Param("amount") BigDecimal amount,
                           @Param("version") Integer version);

    int updateMixCny(@Param("id") Integer id,
                     @Param("amount") BigDecimal amount,
                     @Param("withdraw") BigDecimal withdraw,
                     @Param("version") Integer version);

    int updateMixUsdt(@Param("id") Integer id,
                      @Param("amount") BigDecimal amount,
                      @Param("withdraw") BigDecimal withdraw,
                      @Param("version") Integer version);

    default int updateAmountCnyBuy(UserWalletEntity wallet) {
        return this.updateAmountCny(wallet.getId(), wallet.getAmountCny().negate(), wallet.getVersion(), wallet.getType());
    }

    default int updateAmountUsdtBuy(UserWalletEntity wallet) {
        return this.updateAmountUsdt(wallet.getId(), wallet.getAmountUsdt().negate(), wallet.getVersion(), wallet.getType());
    }

    default int buyProductUpdateAmount(UserWalletEntity wallet, WalletTypeEnum type) {
        if (WalletTypeEnum.CNY.equals(type)) {
            return this.updateAmountCnyBuy(wallet);
        }
        if (WalletTypeEnum.USDT.equals(type)) {
            return this.updateAmountUsdtBuy(wallet);
        }
        throw new RRException("购买类型错误!", -1);
    }
}
